import { sendSuccess, sendFail } from "./utils";
import { getAll } from "./utils/readFile";

export default [
  {
    url: "/api/login",
    timeout: 1000,
    method: "post",
    response: (ctx) => {
      const { userName, password } = ctx.body;
      const list = getAll("user");
      const user = list.find(
        (v) => v.userName === userName && v.password === password
      );
      if (!user) {
        return sendFail(null, "用户不存在");
      }
      const data = user.id + Math.random().toString(36).slice(-8);
      return sendSuccess(data, "登录成功");
    },
  },
  {
    url: "/api/user",
    timeout: 1000,
    method: "get",
    response: (ctx) => {
      const token = ctx.headers.authorization;
      const id = token.substring(0, 1);
      const list = getAll("user");
      const data = list.find((v) => v.id == id);
      return sendSuccess(data);
    },
  },
];
